/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.dao.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.cx1.dao.api.IUserDao;
import org.cx1.model.User;

/**
 *
 * @author tina
 */
public class UserDao implements IUserDao {
    
    private SqlMapClient sqlMapClient;

    public UserDao() {
        sqlMapClient = IbatisConnectionFactory.getSqlMapClient();
    }
   
    @Override
    public String add(User user) {
        String userId = "";
        
        try {
            sqlMapClient.insert("user.add", user);
            userId = user.getId();
            
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return userId;
    }

    @Override
    public int update(User user) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.update("user.update", user);
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }

    @Override
    public User getById(String id) {
        User user = null;
        
        try {
            user = (User) sqlMapClient.queryForObject("user.getById", id);
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return user;
    }

    @Override
    public User getByEmail(String id) {
        User user = null;
        
        try {
            user = (User) sqlMapClient.queryForObject("user.getByEmail", id);
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return user;
    }
    
    @Override
    public List<User> getAll() {
        List<User> users = new ArrayList<User>();
       
        try {
            users = sqlMapClient.queryForList("user.getAll");
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    
        return users;
    }

    @Override
    public int deleteById(String id) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.delete("user.deleteById", id);
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }

    @Override
    public boolean addPhoto(String userId, String photoId) {
        boolean isSuccessful = false;
        
        Map<String,String> map = new HashMap<String, String>();
        map.put("userId", userId);
        map.put("photoId", photoId);

        try {
            sqlMapClient.insert("user.addPhoto", map);
            isSuccessful = true;
            
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return isSuccessful;
    }

    @Override
    public int deletePhoto(String userId, String photoId) {
        int noOfRowsAffected = 0;
        
        Map<String,String> map = new HashMap<String, String>();
        map.put("userId", userId);
        map.put("photoId", photoId);

        
        try {
            noOfRowsAffected = sqlMapClient.delete("user.deletePhoto", map);
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }

    @Override
    public String authenticateUser(String email, String password) {
        Map<String,String> map = new HashMap<String, String>();
        map.put("email", email);
        map.put("password", password);
        
        String userId = "";
        
        try {
            userId = (String) sqlMapClient.queryForObject("user.authenticateUser", map);
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return userId;
    }

    @Override
    public boolean isEmailExisting(String email) {
        User user = null;
        
        try {
            user = (User) sqlMapClient.queryForObject("user.getByEmail", email);
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        if (user != null && !StringUtils.isEmpty(user.getId()) && !StringUtils.isEmpty(user.getEmail())) {
            return true;
        } else {
            return false;
        }
    }
}
